/**
 * 
 */
package org.ejc.struts.statistics;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.ejc.api.registration.RegistrationAPIUtil;
import org.ejc.api.statistics.StatisticsAPIUtil;
import org.ejc.session.EJCSession;
import org.ejc.statistics.charts.BarChart;
import org.ejc.statistics.charts.PieChart;
import org.ejc.statistics.charts.PieChartDoubleValues;

/**
 * @struts.action path="/struts/statistics/statisticsAction" scope="request"
 *                parameter="methodToCall" name="statisticsForm"
 *                validate="false"
 *                input="/struts/statistics/statistics.jsp"
 * 
 * @struts.action-forward name="preStatisticsSuccess" path="/struts/statistics/statistics.jsp"
 * 
 * @author Brian Boyle
 */
public class StatisticsAction extends DispatchAction {

	public final ActionForward preUpdate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		log.debug("About to call the chart constructor");
		HttpSession session = request.getSession();
		EJCSession ejcSession = (EJCSession) session.getAttribute("ejcSession");
		ejcSession.setCurrentAction("Statistics");
		return mapping.findForward("preStatisticsSuccess");
	}
	
	public final ActionForward getRegistrantsPerCountry(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		String PREFIX = "org.ejc.country.";
		HttpSession session = request.getSession();
		EJCSession ejcSession = (EJCSession) session.getAttribute("ejcSession");
		ejcSession.setCurrentAction("Statistics");
		Map countryMap = StatisticsAPIUtil.getLocalHome().create().getCountryStatistics();
		ResourceBundle resource = ResourceBundle.getBundle("org.ejc.resources.Countries", request.getLocale());
		Enumeration iter = resource.getKeys();
		while(iter.hasMoreElements()){
			String resKey = iter.nextElement().toString();
			String countryDesc = (String)resource.getObject(resKey);
			resKey = resKey.replaceAll(PREFIX, "");
			if(countryMap.get(resKey) != null){
				countryMap.put(countryDesc, countryMap.get(resKey));
				countryMap.remove(resKey);
			}
		}
		
		PieChart countryPieChart = new PieChart(countryMap);
		request.setAttribute("pieChartData", countryPieChart);
		request.setAttribute("chartType", "countryPieChart");
		return mapping.findForward("preStatisticsSuccess");
	}
	
	public final ActionForward getGuestStatusStats(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		HttpSession session = request.getSession();
		EJCSession ejcSession = (EJCSession) session.getAttribute("ejcSession");
		ejcSession.setCurrentAction("Statistics");
		Map guestSatusMap = StatisticsAPIUtil.getLocalHome().create().getGuestStatus();
		
		PieChart guestStatusPieChart = new PieChart(guestSatusMap);
		request.setAttribute("pieChartData", guestStatusPieChart);
		request.setAttribute("chartType", "guestStatusPieChart");
		return mapping.findForward("preStatisticsSuccess");
	}
	
	public final ActionForward getGuestLengthOfStayStats(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		HttpSession session = request.getSession();
		EJCSession ejcSession = (EJCSession) session.getAttribute("ejcSession");
		ejcSession.setCurrentAction("Statistics");
		Map guestlengthOfStayMap = StatisticsAPIUtil.getLocalHome().create().getGuestLengthOfStayStats();
		
		BarChart guestStayBarChart = new BarChart(guestlengthOfStayMap);
		String[] seriesNames = guestStayBarChart.seriesNames;
		int n = seriesNames.length;
	    for (int pass=1; pass < n; pass++) {  // count how many times
	        // This next loop becomes shorter and shorter
	        for (int i=0; i < n-pass; i++) {
	            if (Integer.parseInt(seriesNames[i]) > Integer.parseInt(seriesNames[i+1])) {
	                // exchange elements
	                String temp = seriesNames[i];  
	                seriesNames[i] = seriesNames[i+1];  
	                seriesNames[i+1] = temp;
	            }
	        }
	    }
		
		for(int i = 0; i < seriesNames.length; i++){
			if(i == 0){
				seriesNames[i] = seriesNames[i] + " day";
			}else{
				seriesNames[i] = seriesNames[i] + " days";
			}
		}
		request.setAttribute("barChartData", guestStayBarChart);
		request.setAttribute("chartType", "guestlengthOfStayBarChart");
		return mapping.findForward("preStatisticsSuccess");
	}
	
	public final ActionForward getGuestAgeStats(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		HttpSession session = request.getSession();
		EJCSession ejcSession = (EJCSession) session.getAttribute("ejcSession");
		ejcSession.setCurrentAction("Statistics");
		Map guestAgeStatsMap = StatisticsAPIUtil.getLocalHome().create().getGuestAgeStats();
		
		BarChart guestAgeBarChart = new BarChart(guestAgeStatsMap);
		
		request.setAttribute("barChartData", guestAgeBarChart);
		request.setAttribute("chartType", "guestAgesBarChart");
		return mapping.findForward("preStatisticsSuccess");
	}
	
	public final ActionForward getMoneyAmountPerCountry(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		String PREFIX = "org.ejc.country.";
		HttpSession session = request.getSession();
		EJCSession ejcSession = (EJCSession) session.getAttribute("ejcSession");
		ejcSession.setCurrentAction("Statistics");
		Map countryMap = StatisticsAPIUtil.getLocalHome().create().getMoneyAmountPerCountryStats();
		ResourceBundle resource = ResourceBundle.getBundle("org.ejc.resources.Countries", request.getLocale());
		Enumeration iter = resource.getKeys();
		while(iter.hasMoreElements()){
			String resKey = iter.nextElement().toString();
			String countryDesc = (String)resource.getObject(resKey);
			resKey = resKey.replaceAll(PREFIX, "");
			if(countryMap.get(resKey) != null){
				countryMap.put(countryDesc, countryMap.get(resKey));
				countryMap.remove(resKey);
			}
		}
		
		PieChartDoubleValues moneyPerCountryPieChart = new PieChartDoubleValues(countryMap);
		request.setAttribute("pieChartData", moneyPerCountryPieChart);
		request.setAttribute("chartType", "moneyPerCountryPieChart");
		return mapping.findForward("preStatisticsSuccess");
	}
	
	

}
